{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4562f829-9a7d-4b46-841e-6da08f6ba19b",
   "metadata": {},
   "source": [
    "# Robodyno Proximity Sensor API"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c358146e-c15d-42a9-b2be-0bc27ffd0e2f",
   "metadata": {},
   "source": [
    "## 1.初始化接近开关传感器对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adafa235-3810-4c44-a991-f48127e79a18",
   "metadata": {},
   "source": [
    "`ProximitySensor(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : 悬崖传感器模块ID，默认`0x36`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "72695494-0cae-4241-96ff-e6c3c039ba0a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.components import ProximitySensor\n",
    "from robodyno.interfaces import CanBus\n",
    "can = CanBus()\n",
    "proximity_sensor = ProximitySensor(can, 0x36)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf0f19f5-4e17-4153-9406-c72824f976a2",
   "metadata": {},
   "source": [
    "## 2.读取接近开关传感器版本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43a09867-113b-4aad-9a1f-e50b2729057f",
   "metadata": {},
   "source": [
    "`get_version(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- timeout : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "- API版本dict\n",
    "    - `main_version` : 主版本号\n",
    "    - `sub_version` : 副版本号\n",
    "    - `type` : 设备类型\n",
    "        - ROBODYNO_PROXIMITY_SENSOR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1b9f64d-75c5-43ba-aa46-6170b1ffc4ea",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'main_version': 0,\n",
       " 'sub_version': 1,\n",
       " 'data': 0.0,\n",
       " 'type': <Model.ROBODYNO_PROXIMITY_SENSOR: 166>}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "proximity_sensor.get_version(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a969fa2e-1358-417b-b9a6-9c913bea62c9",
   "metadata": {},
   "source": [
    "## 3.设置CAN_ID"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e02704-443b-4fc2-9113-711a0f07ce78",
   "metadata": {},
   "source": [
    "`config_can_bus(new_id, bitrate = 1000000)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `new_id` ：接近开关传感器的新CAN_ID (默认：`0x36`)\n",
    "- `bitrate` : CAN总线通讯速率\n",
    "    - 1000000\n",
    "    - 500000\n",
    "    - 250000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b7c89056-933d-4802-99ba-781efc787aec",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "proximity_sensor.config_can_bus(0x36)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2314b5db-ce65-42e4-89fc-64f7a0537bff",
   "metadata": {},
   "source": [
    "## 4.读取接近开关当前状态"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d21c834-ab83-456f-877e-65658eeb7237",
   "metadata": {},
   "source": [
    "`get_data(timeout=0.02)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- `timeout` : 请求超时时间(s)\n",
    "\n",
    "返回值:\n",
    "\n",
    "- 接近开关状态值。\n",
    "  - 0/1，如果值为0，表示与金属物体已接触，否则为1，\n",
    "  - 如果读取超时，则为`None`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c7cd9d41-d45b-4bc2-ab71-a98279b0ec0a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "proximity_sensor.get_data()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
